(function(){
	function stopProcess(e){
		e.stopPropagation();
	}
	
	var toolHandle={
		createSelectItem:function(cfg){
			var len=cfg.value.length;
			var str="";
			for(var i=0;i<len;i++){
				str+=cfg.func(cfg.value[i]);
			}
			return $(str);
		},
		bindBlock:function(val){
			return "<"+val+">"+val+"</"+val+">";
		},
		bindFont:function(val){
			return "<span style='font-family:"+val+"'>"+val+"</span>";
		},
		bindFontSize:function(val){
			return "<span style='font-size:"+val+"'>"+val+"</span>";
		},
		bindColor:function(val){
			return "<span style='background-color:"+val+"'>"+val+"</span>";
		},
		bindJustify:function(val){
			return "<span style='text-align:"+val+"'>"+val+"</span>";
		},
		bindBold:function(val){
			return "<span style='font-weight:"+val+"'>"+val+"</span>";
		},
		bindItalic:function(val){
			return "<span style='font-style:"+val+"'>"+val+"</span>";
		},
		bindTextDec:function(val){
			return "<span style='text-decoration:"+val+"'>"+val+"</span>";
		},
		bindJustify:function(val){
			return "<span style='text-align:"+val+"'>"+val+"</span>";
		},
		bindIndent:function(val){
			return "<span style='text-indent:"+val+"'>"+val+"</span>";
		},
		bindCss:function(val,css){
			return "<span style='"+css+":"+val+"'>"+val+"</span>";
		}
	}
	var toolbar={
		formatblock:{
			title:'段落',
			value:['H1','H2','H3','H4'],
			isTag:true,
			func:toolHandle.bindBlock
		},
		indent:{
			title:'首行缩进',
			value:['10px','20px','30px'],
			css:'text-indent',
			jsCss:'textIndent',
			func:toolHandle.bindIndent
		},
		fontname:{
			title:'字体',
			css:'font-family',
			jsCss:'fontFamily',
			value:['SimSun','NSimSun','FangSong_GB2312','KaiTi_GB2312','SimHei',
				   'Microsoft YaHei','Arial','Arial Black','Times New Roman'],
		    func:toolHandle.bindFont	   
		},
		fontsize:{
			title:'文字大小',
			css:'font-size',
			jsCss:'fontSize',
			func:toolHandle.bindFontSize,	   
			value:['12px','14px','16px','18px','20px','22px','24px','26px']
		},
		fontcolor:{
			title:'文字颜色',
			css:'color',
			jsCss:'color',
			func:toolHandle.bindColor,	  
			value:['#E53333','#E56600','#FF9900','#64451D','#DFC5A4','#FFE500',
				   '#009900','#006600','#99BB00','#B8D100','#60D978','#00D5FF',
				   '#337FE5','#003399','#4C33E5','#9933E5','#CC33E5','#EE33EE',
				   '#FFFFFF','#CCCCCC','#999999','#666666','#333333','#000000'
				   ]
		},
		bgcolor:{
			title:'背景颜色',
			css:'background-color',
			jsCss:'backgroundColor',
			defText:'A',
			func:toolHandle.bindColor,	  
			value:['#E53333','#E56600','#FF9900','#64451D','#DFC5A4','#FFE500',
				   '#009900','#006600','#99BB00','#B8D100','#60D978','#00D5FF',
				   '#337FE5','#003399','#4C33E5','#9933E5','#CC33E5','#EE33EE',
				   '#FFFFFF','#CCCCCC','#999999','#666666','#333333','#000000'
				   ]
		},
		bold:{
			title:'文字加粗',	
			value:['normal','bold'],
			css:'font-weight',
			jsCss:'fontWeight',
			func:toolHandle.bindBold	
		},
		italic:{
			title:'斜体',	
			value:['normal','italic'],
			css:'font-style',
			jsCss:'fontStyle',
			func:toolHandle.bindItalic
		},
		textDecoration:{
			title:'分割线',	
			value:['underline','line-through'],
			css:'text-decoration',
			jsCss:'textDecoration',
			func:toolHandle.bindTextDec	  
		},
		justify:{
			title:'对齐',
			css:'text-align',
			jsCss:'textAlign',
			value:['left','center','right'],
			func:toolHandle.bindJustify	  
		},
		width:{
			title:'宽度',
			css:'width',
			jsCss:'width',
			value:[],
			func:toolHandle.bindCss	
		},
		height:{
			title:'高度',
			css:'height',
			jsCss:'height',
			value:[],
			func:toolHandle.bindCss	
		},
		lineHeight:{
			title:'行间距',
			css:'line-height',
			jsCss:'lineHeight',
			value:['18px','22px'],
			func:toolHandle.bindCss	
		}
	};
	var curSelect=null;
	function showSelect(e){
		stopProcess(e);
		$(document).trigger("click");
		var src=$(this);
		var key=src.attr("key");
		curSelect=src.parent().children("div.tool_select_div");
		curSelect.show();
	}
	function hideSelect(e){
		if(curSelect)
			curSelect.hide();
	}
	function checkItem(e){
		$.initEvent(e);
		var src=$(e.src);
		var select=$(this);
		var input=select.parent().children("input");
		if(input.val()==src.text())
			return ;
		var key=input.attr("key");
		var cfg=toolbar[key];
		select.children(".check").removeClass("check");
		src.addClass("check");
		input.val(src.text());
	}
	window.simpleText={
		//初始化 生成input+select 形式
		init:function(elem){
			if(!elem||!elem.length)
				return;
			for(var item in toolbar){
				var cfg=toolbar[item];
				var span=$("<span class='toolSpan'></span>").appendTo(elem);
				var label=$("<label>"+cfg.title+"</label>").appendTo(span);
				var input=$("<input id='tool_"+item+"' key="+item+"></input>").appendTo(span);
				var select=$("<div style='display:none' class='tool_select_div' key="+item+" />").appendTo(span);
				toolHandle.createSelectItem(cfg).appendTo(select);
				input.bind("click",showSelect);
				select.bind("click",checkItem);
			}
			$(document).bind("click",hideSelect);
		},
		addTool:function(key,obj){
			toolbar[key]=obj;
		},
		createHtml:function(val,elem){
			var span=$("<div class='_simple_text_div_'></div>");
			var lastItem=span;
			elem.find("input").each(function(){
				var input=$(this);
				if(input.val().trim()){
					var key=input.attr("key");
					var cfg=toolbar[key];
					if(cfg.isTag){
						lastItem=$("<"+input.val()+">").appendTo(span);
					}
					else{
						span.css(cfg.css,input.val());
					}
				}
			});
			if(typeof val!="string")
				lastItem.append(val);
			else
				lastItem.html(val.replace(/\n/g,"<br>").replace(/\s/g,"&nbsp"));
			return span;
		}
	}
	
})();